package com.igroupes.ovineherd.model.attribute.number;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.NumberStyle;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.List;

/**
 * 数字
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class NumberModelData extends NumberBasicAttribute{

    private static String REGEX = "^[0-9]*$";
    public static final Integer MAXAGE = 200;
    public static final Integer MINAGE = 1;
    public NumberModelData() {
        super();
    }

    public Boolean verifyField(NumberModelData json) {
        return super.verifyField(json);
    }

    @Override
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        NumberModelData numberModelData = (NumberModelData) parentModelData.getAttributes();
        List<FieldQuery> fieldQueries = super.sqlField(parentModelData,dataModelAttributeEntityOld);
        FieldQuery fieldQuery = fieldQueries.get(0);
        DataModelAttributeEntity dataModelAttributeEntity = fieldQuery.getDataModelAttributeEntity();
        fieldQuery.setDataModelAttributeEntity(dataModelAttributeEntity);
        fieldQueries.set(0,fieldQuery);
        return fieldQueries;
    }
    @Override
    public JSONObject getAttribute(ModelDataJsonAttributeQuery modelDataJsonAttributeQuery) {
        return super.getAttribute(modelDataJsonAttributeQuery);
    }
    @Override
    public JSONObject getAttributeStyle(ModelDataJsonAttributeQuery dataModelAttributeEntity) {

        JSONObject jsonObject = dataModelAttributeEntity.getJson();
        Integer minValue = Convert.toInt(jsonObject.get("minValue"));
        Integer maxValue = Convert.toInt(jsonObject.get("maxValue"));
        return new JSONObject(new NumberStyle(
                dataModelAttributeEntity.getName(),"",
                dataModelAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode()),
                minValue, maxValue
        ), false);
    }
}
